﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Web;
using Univar.Helpers;

namespace Univar
{
	public class PersistentPropertyStore<T> : DynamicStore<T>
	{
		private string _storageParentKey;
		private string _propertyName;
		private object[] _controlsToPersist;
		//public string DefaultstorageParentKey = "PersistentProperty";

		public PersistentPropertyStore(params object[] controlsToPersist)
			: this("Text", null, Sources.Session, controlsToPersist)
		{ }
		public PersistentPropertyStore(Sources sourceType, params object[] controlsToPersist)
			: this("Text", null, sourceType, controlsToPersist)
		{ }

		public PersistentPropertyStore(string propertyName, params object[] controlsToPersist)
			: this(propertyName, null, Sources.Session, controlsToPersist)
		{ }

		public PersistentPropertyStore(string propertyName, Sources sourceType, params object[] controlsToPersist)
			: this(propertyName, null, sourceType, controlsToPersist)
		{ }

		public PersistentPropertyStore(string propertyName, string storageParentKey, Sources sourceType, params object[] controlsToPersist)
			: base(default(T), storageParentKey, null, null, null, null, sourceType)
		{
			_storageParentKey = storageParentKey != null 
				? storageParentKey
				: StorageUser.Context.Request.AppRelativeCurrentExecutionFilePath.Substring(2).Replace('/', Global.KeyDelimiter);
			_propertyName = propertyName;
			_controlsToPersist = controlsToPersist;
		}

		public static void Persist(string propertyName, string storageParentKey, Sources sourceType, bool isPostBack, params object[] controlsToPersist)
		{
			new PersistentPropertyStore<T>(propertyName, storageParentKey, sourceType, controlsToPersist).Persist(isPostBack);
		}

		public static void Persist(string propertyName, Sources sourceType, bool isPostBack, params object[] controlsToPersist)
		{
			new PersistentPropertyStore<T>(propertyName, null, sourceType, controlsToPersist).Persist(isPostBack);
		}

		public static void PersistToAllUsers(string propertyName, string storageParentKey, bool isPostBack, params object[] controlsToPersist)
		{
			new PersistentPropertyStore<T>(propertyName, storageParentKey, Sources.InFileCache, controlsToPersist).Persist(isPostBack);
		}

		public void Persist(bool isPostBack)
		{
			string key;
			foreach (Control control in _controlsToPersist)
			{
				key = _storageParentKey + Global.KeyDelimiter + control.ID;
				if (isPostBack)
				{
					T value = Reflector.GetPropertyValue<T>(control, _propertyName);
					SetValue(key, value);
				}
				else
				{
					T value = GetValue(key);
					if (value != null)
						Reflector.SetPropertyValue<T>(control, _propertyName, value, true);
				}
			}
		}

		public static void Persist(string propertyName, string storageKey, bool isPostBack, Sources sourceType, Control controlToPersist)
		{
			Persist(default(T), propertyName, storageKey, isPostBack, sourceType, controlToPersist);
		}

		public static void Persist(T defaultValue, string propertyName, string storageKey, bool isPostBack, Sources sourceType, Control controlToPersist)
		{
			DynamicStore<T> obj = new DynamicStore<T>(defaultValue, storageKey, sourceType);
			if (isPostBack)
			{
				T value = Reflector.GetPropertyValue<T>(controlToPersist, propertyName);
				obj.Value = value;
			}
			else
			{
				T value = obj.Value;
				if (value != null)
					Reflector.SetPropertyValue<T>(controlToPersist, propertyName, value, true);
			}
		}
	}
}
